package com.niubi.yygh.orders.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.niubi.yygh.common.result.R;
import com.niubi.yygh.enums.OrderStatusEnum;
import com.niubi.yygh.model.order.OrderInfo;
import com.niubi.yygh.orders.service.OrderInfoService;
import com.niubi.yygh.vo.order.OrderCountQueryVo;
import com.niubi.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.AuthContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 牛俊杰
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {

  @Autowired
  private OrderInfoService orderInfoService;

  @ApiOperation(value = "创建订单")
  @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
  public R submitOrder(
          @ApiParam(name = "scheduleId", value = "排班id", required = true)
          @PathVariable String scheduleId,
          @ApiParam(name = "patientId", value = "就诊人id", required = true)
          @PathVariable Long patientId) {

    Long orderId = orderInfoService.saveOrder(scheduleId, patientId);
    return R.ok().data("orderId",orderId);
  }

  //订单列表（条件查询带分页）
  @GetMapping("auth/{page}/{limit}")
  public R list(@PathVariable Long page,
                @PathVariable Long limit,
                OrderQueryVo orderQueryVo, HttpServletRequest request) {
    //获取用户id
    orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
    Page<OrderInfo> pageParam = new Page<>(page,limit);
    IPage<OrderInfo> pageModel =
            orderInfoService.selectPage(pageParam,orderQueryVo);
    return R.ok().data("pageModel",pageModel);
  }
  @ApiOperation(value = "获取订单状态")
  @GetMapping("auth/getStatusList")
  public R getStatusList() {
    return R.ok().data("statusList", OrderStatusEnum.getStatusList());
  }

  //根据订单id查询订单详情
  @GetMapping("auth/getOrders/{orderId}")
  public R getOrders(@PathVariable Long orderId) {
    OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);
    return R.ok().data("orderInfo",orderInfo);
  }

  @ApiOperation(value = "取消预约")
  @GetMapping("auth/cancelOrder/{orderId}")
  public R cancelOrder(@PathVariable Long orderId){
    Boolean flag = orderInfoService.cancelOrder(orderId);
    return R.ok().data("flag",flag);
  }

  @ApiOperation(value = "获取订单统计数据")
  @PostMapping("inner/getCountMap")
  public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
    return orderInfoService.getCountMap(orderCountQueryVo);
  }
}


